package com.lucy.nepu.module.paper.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.domain.PageResultDTO;
import com.lucy.common.exception.BusinessException;
import com.lucy.nepu.module.paper.dao.PaperDAO;
import com.lucy.nepu.module.paper.entity.Paper;
import com.lucy.nepu.module.paper.event.PaperCreatedEvent;
import com.lucy.nepu.module.paper.event.PaperDeletedEvent;
import com.lucy.nepu.module.paper.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class PaperServiceImpl implements PaperService {
    @Autowired
    PaperDAO dao;

    @Resource
    ApplicationContext context;

    @Override
    public List<Paper> batchGet(List<String> list) {
        return null;
    }

    @Override
    public void batchDelete(List<String> list) {

    }

    @Override
    public Paper getOneUnwrap(String s) {
        Optional<Paper> paperOp = dao.findById(s);
        if (!paperOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        return paperOp.get();
    }


    @Override
    public PageResultDTO<Paper> getAll(Pageable pageable, Example<Paper> example) {
        Page<Paper> page;

        if(Validator.isEmpty(example)){
            page = dao.findAll(pageable);
        }
        else {
            page = dao.findAll(example,pageable);
        }
        /*
          获取分页主体内容
         */
        List<Paper> list = page.getContent();
        PageResultDTO<Paper> pageResultDTO = new PageResultDTO<>();


        //封装分页请求结果
        pageResultDTO.setContent(list);
        pageResultDTO.setSize(page.getSize());
        pageResultDTO.setTotalPages(page.getTotalPages());
        pageResultDTO.setNumber(page.getNumber());
        pageResultDTO.setTotalElements(page.getTotalElements());
        return pageResultDTO;
    }

    @Override
    public List<Paper> getExample(Example<Paper> example) {
        return dao.findAll(example);
    }

    @Override
    public void deleteOne(String id) {
        Paper paper = getOneUnwrap(id);
        dao.deleteById(id);
        // 发布事件
        context.publishEvent(new PaperDeletedEvent(this,paper));
    }

    @Override
    public Optional<Paper> getOne(String id) {
        return dao.findById(id);
    }

    @Override
    public Paper saveOne(Paper o) {
        if (Validator.isNotEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"新建DTO不可携带ID");
        }
        Paper saveRes = dao.save(o);
        // 发布事件
        context.publishEvent(new PaperCreatedEvent(this,saveRes));
        return saveRes;
    }

    @Override
    public Paper updateOne(Paper o) {
        if (Validator.isEmpty(o.getId())){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"更新DTO必须携带ID");
        }
        return dao.save(o);
    }
}
